package tt.dz.mvc.controller.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;

import tt.dz.entity.base.JsonForList;
import tt.dz.entity.base.OmsCommonParam;
import tt.dz.entity.system.OmsOperList;

public class BaseAction 
{
	public static final class CommonParam
	{
		public static final String SORT_NAME   = "sortName";
		public static final String SORT_VALUE  = "sortValue";
		public static final String PAGE_NO         = "pageNo";
		public static final String PAGE_SIZE      = "pageSize";
		public static final String SESSION_USERID = "login_oper_id";
		public static final String SESSION_USER = "loginUser";
	}
	/**
	 * 构建公共分页查询参数
	 * @param request
	 * @param isQueryTotal
	 * @return
	 */
	public OmsCommonParam buildQueryParam(HttpServletRequest request,boolean isQueryTotal)
	{
		OmsCommonParam returnParam = new OmsCommonParam();
		returnParam.setQueryTotal(isQueryTotal);
		returnParam.setSortName(request.getParameter(BaseAction.CommonParam.SORT_NAME));
		String sortValue = request.getParameter(BaseAction.CommonParam.SORT_VALUE);
		String pageNo = request.getParameter(BaseAction.CommonParam.PAGE_NO);
		String pageSize = request.getParameter(BaseAction.CommonParam.PAGE_SIZE);
		if (StringUtils.isNumeric(sortValue))
		{
			returnParam.setSortValue(Integer.parseInt(sortValue));
		}
		
		if (StringUtils.isNumeric(pageNo) && StringUtils.isNumeric(pageSize))
		{
			returnParam.setPageNo(Integer.parseInt(pageNo));
			returnParam.setPageSize(Integer.parseInt(pageSize));
		}
		
		return returnParam;
	}
	
	public JsonForList<?> buildOtherJsonForList(String msgCode,String msg)
	{
		JsonForList<?> jfl = new JsonForList<>();
		jfl.setCode(msgCode);
		jfl.setMsg(msg);
		return jfl;
	}
	
	public String getLoginUserId(HttpServletRequest request)
	{
		HttpSession session = request.getSession(); 
		return (String) session.getAttribute(BaseAction.CommonParam.SESSION_USERID); 
	}
	
	public OmsOperList getLoginUser(HttpServletRequest request)
	{
		HttpSession session = request.getSession(); 
		return (OmsOperList) session.getAttribute(BaseAction.CommonParam.SESSION_USER); 
	}
	
	

}
